package study.JavaBase;

/**
 * date:2025.10.13
 * title:java循环结构
 * author:fzy
 */
public class Java_6 {
    public static void main(String[] arg) {
        LoopClass.whileTest();
        LoopClass.doWhileTest();
        LoopClass.forTest();
    }
}


/**
 * 循环结构
 * 增强行for类似于js中的forEach，专门用于数组的循环
 * 循环中的跳出有return，break，continue
 * return 跳出整个函数
 * break 跳出整个循环
 * continue 跳出当前循环
 */
class LoopClass {
    //while循环
    static void whileTest() {
        System.out.println("----->while");
        int a = 0;
        while (a <= 5) {
            System.out.println(a);
            if(a==3){
                //不执行循环外的函数
                return;
            }
            a++;
        }

    }

    //dowhile
    static void doWhileTest() {
        int a = 0;
        System.out.println("----->dowhile");
        do {
            System.out.println(a);
            if(a==3){
                break;
            }
            a++;
        } while (a <= 5);
    }

    //for
    static void forTest() {
        System.out.println("----->for");
        for (int a = 0; a <= 5; a++) {
            if(a==2){
                continue;
            }
            System.out.println(a);
        }
    }

    static void enhanceForTest() {
        System.out.println("----->enhanceForTest");
        int[] numbers = {1, 2, 3, 4, 5};
        for (int i : numbers) {
            System.out.println(i);
        }

    }
}